﻿using Mengsk.ProxyDelivery.Domain;
using Mengsk.ProxyDelivery.Service.Delivery;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Mengsk.ProxyDelivery.App.UI.Delivery
{
    /// <summary>
    /// Interaction logic for DeliveryExportWindow.xaml
    /// </summary>
    public partial class DeliveryExportWindow : Window
    {
        public DeliveryOut[] Outs { get; set; }

        public DeliveryExportWindow()
        {
            InitializeComponent();
        }

        private void btnCount_Click(object sender, RoutedEventArgs e)
        {
            this.Export(ExportType.COUNT);
        }

        private void btnWeight_Click(object sender, RoutedEventArgs e)
        {
            this.Export(ExportType.WEIGHT);
        }

        private void Export(ExportType type)
        {
            try
            {
                var dates = Outs.Select(obj => new DateTime(obj.CreateTime.Year, obj.CreateTime.Month, 1)).Distinct().ToArray();
                string dateStr = dates[0].ToString("yyyy年MM月");
                if (dates.Length > 1)
                {
                    dateStr += "-" + dates.Last().ToString("yyyy年MM月");
                }
                dateStr += " 物流费用统计";
                Microsoft.Win32.SaveFileDialog sfd = new Microsoft.Win32.SaveFileDialog();
                sfd.FileName = "曹家林" + dateStr + ".xls";
                bool? ret = sfd.ShowDialog();

                if (ret == null || ret.Value == false)
                {
                    return;
                }
                ExportArgs args = new ExportArgs { FilePath = sfd.FileName, Type = type, StartWeight = float.Parse(this.tbWeight.Text.Trim()), MoreWeight = float.Parse(this.tbMoreWeight.Text.Trim()) };
                DeliveryService.Export(args, Outs);
                this.DialogResult = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }
}
